package com.originskyseed.platform.user.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.common.api.util.QueryAppUtil;
import com.originskyseed.platform.common.utils.JsonUtils;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.Anchor;
import com.originskyseed.platform.user.model.Family;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IAnchorService;
import com.originskyseed.platform.user.service.IFamilyService;
import com.originskyseed.platform.user.vo.request.FamilyRequestVO;

/**
 * 家族信息Controller类（OSI_FAMILY）
 *
 * @author tangwanghui
 */
@Controller
@RequestMapping("/family")
@BaseLocaleResource(localeFileName = "i18n_user")
public class FamilyController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(FamilyController.class);

	@DubboReference
	private IFamilyService familyService;

	@DubboReference
	private IAnchorService anchorService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/familyList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<Family>> familyList(HttpServletRequest request) {
		BaseResponseBody<BasePage<Family>> result = null;
		try {
			// 请求对象
			FamilyRequestVO requestVO = this.getParameterBean(FamilyRequestVO.class);

			// 分页查询
			BasePage<Family> page = new BasePage<Family>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.familyService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<Family>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<Family>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 开放平台选择家族列表分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectfamilyList.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<Family>> selectfamilyList(HttpServletRequest request) {
		BaseResponseBody<BasePage<Family>> result = null;
		try {
			// 请求对象
			FamilyRequestVO requestVO = this.getParameterBean(FamilyRequestVO.class);

			// 分页查询
			BasePage<Family> page = new BasePage<Family>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.familyService.selectFamilyListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<Family>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<Family>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/familySave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<Family> familySave(HttpServletRequest request) {
		BaseResponseBody<Family> result = null;
		try {
			// 请求对象
			Family family = this.getParameterBean(Family.class);

			if (StringUtils.isNotBlank(family.getId())) {
				this.familyService.updateByPrimaryKey(family);
			} else {
				this.familyService.insertSelective(family);
			}
			result = new BaseResponseBody<Family>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), family);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Family>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/familyDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> familyDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.familyService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 家族信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/familyInfo.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<Family> familyInfo(HttpServletRequest request) {
		BaseResponseBody<Family> result = null;
		try {
			// 请求参数
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);

			if (userInfo == null || userInfo.getOtherAccount() == null || StringUtils.isEmpty(userInfo.getOtherAccount())) {
				result = new BaseResponseBody<Family>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.ANCHOR_BIND_ISNULL.getMsg()), null);
				return result;
			}
			Anchor anchor = this.anchorService.selectByAnchorId(userInfo.getOtherAccount());
			// 请求接口查询家族信息
			if (anchor != null && anchor.getFamilyId() != null) {
				TreeMap<String, Object> paramMap = new TreeMap<>();
				Family myFamily = new Family();
				paramMap.put("familyId", anchor.getFamilyId());
				Map resultMap = QueryAppUtil.query("familyInfoUrl", 1, paramMap);
				Integer code = Integer.parseInt(resultMap.get("code").toString());
				if (code == 1) { // 成功
					String data = (String) resultMap.get("data");
					// json字符串转object
					myFamily = JsonUtils.parse(data, Family.class);
					myFamily.setFamilyId(anchor.getFamilyId());
				} else {
					return new BaseResponseBody<Family>(false, CommonResponseCode.ERROR.getCode(),(String)resultMap.get("msg"), null);
				}
				result = new BaseResponseBody<Family>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.SELECT_DATA_SUCCESS.getMsg()), myFamily);
			} else {
				result = new BaseResponseBody<Family>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.ANCHOR_BELONG_FAMILY_ISNULL.getMsg()), null);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Family>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.SELECT_DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
